#ifndef A13DD733_51DD_40E1_9736_3F927E91ECCF
#define A13DD733_51DD_40E1_9736_3F927E91ECCF

#include <iostream>
#include "protocal.hpp"

enum {
	DIVERR = 1,
	MODERR
};

class ServerCal{
public:
	Response Calculatorhelp(Request& rq)
	{
		Response rsp;
		switch(rq.op){
			case '+':
				rsp.result = rq.x + rq.y;
				break;
			case '-':
				rsp.result = rq.x - rq.y;
				break;
			case '*':
				rsp.result = rq.x * rq.y;
				break;
			case '/':
				if(rq.y == 0){
					rsp.code = DIVERR;
				}
				else{
					rsp.result = rq.x / rq.y;
				}
				break;
			case '%':
				if(rq.y == 0){
					rsp.code = MODERR;
				}
				else{
					rsp.result = rq.x % rq.y;
				}
				break;
			default:
				break;
		}
		return rsp;
	}
	std::string Calculator(std::string& package)
	{
		// std::cout << package << std::endl;
		std::string content;
		bool r = Decode(package,&content);
		// std::cout << r << std::endl;
		if(!r) return "";
		Request rq;
		r = rq.Deserialize(content);
		// std::cout << r << std::endl;
		if(!r) return "";
		Response rsp = Calculatorhelp(rq);
		content = "";
		rsp.Serialize(&content);
		Encode(content);
		return content;
	}
};

#endif /* A13DD733_51DD_40E1_9736_3F927E91ECCF */
